/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast.view;

import java.util.Iterator;
import org.chaoticengine.ast.GlobalData;
import org.chaoticengine.ast.model.Location;
import org.chaoticengine.ast.model.TradeGood;
import org.chaoticengine.ast.model.TradeInventory;
import org.chaoticengine.ast.model.World;
import org.chaoticengine.ast.pattern.ViewController;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class LocationDetailsView extends ViewController<Location> {
    protected Location model = null;
    protected String name = "";
    protected String alt = "";
    protected int pop = 0;
    protected TradeInventory goods = null;

    @Override
    public Location getModel() {
        return(model);
    }

    @Override
    public void keyPressed(int key, char c) {}

    @Override
    public void keyReleased(int key, char c) {}

    @Override
    public void onMouseClick(int x, int y, int button) {}

    @Override
    public void onMouseEnter() {}

    @Override
    public void onMouseExit() {}

    @Override
    public void onMouseMove(int oldx, int oldy, int newx, int newy) {}

    @Override
    public void onMouseRelease(int x, int y, int button) {}

    @Override
    public void onMouseWheelDown() {}

    @Override
    public void onMouseWheelUp() {}

    @Override
    public void refresh(Location loc) {
        model = loc;

        name = loc.getName();
        alt = loc.getAltitude().toString();
        pop = loc.getPopulation();
        goods = loc.getGoods();
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        // Check / get data
        if (model == null) {
            return;
        }

        World world = GlobalData.getInstance().getWorld();

        // Render
        grphcs.translate(position.x, position.y);
        grphcs.setColor(Color.white);
        int line = 1;
        grphcs.drawString("Date: " + world.getDay(), grphcs.getFont().getLineHeight(), grphcs.getFont().getLineHeight() * line++);
        grphcs.drawString(name, grphcs.getFont().getLineHeight(), grphcs.getFont().getLineHeight() * line++);
        grphcs.drawString("Altitude: " + alt, grphcs.getFont().getLineHeight(), grphcs.getFont().getLineHeight() * line++);
        if (pop >= 0) {
            grphcs.drawString("Population: " + pop, grphcs.getFont().getLineHeight(), grphcs.getFont().getLineHeight() * line++);
        }

        if (goods != null) {
            Iterator<TradeGood> git = goods.getGoods();
            while (git.hasNext()) {
                TradeGood good = git.next();
                grphcs.drawString(good.getName() + ": " + goods.getQuantity(good),
                        grphcs.getFont().getLineHeight(),
                        grphcs.getFont().getLineHeight() * line++);
            }
        }
        
        grphcs.translate(-position.x, -position.y);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) {}

    @Override
    public void onDeselect() {
    }

    @Override
    public void onSelect() {
    }
}
